﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rabbit.Model; 

namespace Rabbit.BusinessLogicLayer
{
    public class PublicStatusBL : AutoUpdateMessageClassBase<Status>
    {
        public PublicStatusBL(double UpdateInterval)
            : base(UpdateInterval)
        {

        }

        #region GetPublicStatus
        private static List<Status> GetPublicStatuses()
        {
            string statusString = RequestHelper.SendRequest("statuses/public_timeline.json", "");
            List<Status> publicStatuses = WrappedJsonHelper.Deserialize<List<Status>>(statusString);
            StatusesBL.GetForwardedStatusPicture(publicStatuses);
            return publicStatuses;
        }
        #endregion

        protected override void FetchNewMessage()
        {
            List<Status> newStatusList = GetPublicStatuses();
            newStatusList = newStatusList.TakeWhile(s => s.Id > LastMaxID).ToList();
            if (newStatusList.Count>0)
            {
                LastMaxID = newStatusList.Max(s => s.Id);
            }
            RaiseNewMessageReceivedEvent(newStatusList);
        }

        protected override void FetchMessage()
        {
            List<Status> tempStatus = GetPublicStatuses();
            LastMaxID = tempStatus.Max(s => s.Id);
            RaiseMessageFetchedEvent(tempStatus);
        }

        public override List<Status> GetNextPageMessage()
        {
            throw new NotImplementedException();
        }

        public override List<Status> GetPreviousePageMessage()
        {
            throw new NotImplementedException();
        }

        public override List<Status> GetFirstPageMessage()
        {
            throw new NotImplementedException();
        }
    }
}
